package com.kkd.kkdapp.http.entity;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import android.text.TextUtils;

public class RepaymentHistoryDataBean {
	/**
	 * 类型--每个月的标题
	 */
	public static final int HEAD = 0;
	/**
	 * 类型--每个月的条目
	 */
	public static final int ITEM = 1;

	/**
	 * 类型：标题或者条目
	 */
	private int type;

	/**
	 * 标题实体
	 */
	private RepaymentHistoryHeadData headData;

	/**
	 * 标题下的item实体
	 */
	private CalendarListDataItem itemData;

	/**
	 * 创建head时调用
	 * 
	 * @param type
	 * @param headData
	 */
	public RepaymentHistoryDataBean(int type, RepaymentHistoryHeadData headData) {
		this.type = type;
		this.headData = headData;
	}

	/**
	 * 创建item时调用
	 * 
	 * @param type
	 * @param itemData
	 */
	public RepaymentHistoryDataBean(int type, CalendarListDataItem itemData) {
		this.type = type;
		this.itemData = itemData;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public RepaymentHistoryHeadData getHeadData() {
		return headData;
	}

	public void setHeadData(RepaymentHistoryHeadData headData) {
		this.headData = headData;
	}

	public CalendarListDataItem getItemData() {
		return itemData;
	}

	public void setItemData(CalendarListDataItem itemData) {
		this.itemData = itemData;
	}

	/**
	 * 将List<CalendarListDataItem>封装成ArrayList<RepaymentHistoryDataBean>
	 * 
	 * @param list_items
	 * @return
	 */
	public static ArrayList<RepaymentHistoryDataBean> getDataList(
			List<CalendarListDataItem> list_items) {
		// 最后要返回的list
		ArrayList<RepaymentHistoryDataBean> list = new ArrayList<RepaymentHistoryDataBean>();
		// 将head和其所属的items关联起来的map
		Map<RepaymentHistoryHeadData, List<CalendarListDataItem>> map = new LinkedHashMap<RepaymentHistoryHeadData, List<CalendarListDataItem>>();

		RepaymentHistoryHeadData headData = new RepaymentHistoryHeadData();
		for (int i = 0; i < list_items.size(); i++) {
			try {
				String monthStat = list_items.get(i).getMonthStat();
				if (!TextUtils.isEmpty(headData.getMonthStat())
						&& !monthStat.equals(headData.getMonthStat())) {
					headData = new RepaymentHistoryHeadData();
				}
				headData.setMonthStat(monthStat);
				List<CalendarListDataItem> items = map.get(headData);
				if (items == null) {
					items = new ArrayList<CalendarListDataItem>();
				}
				items.add(list_items.get(i));
				map.put(headData, items);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		// 用迭代器遍历map添加到list里面
		Iterator<Entry<RepaymentHistoryHeadData, List<CalendarListDataItem>>> iterator = map
				.entrySet().iterator();
		while (iterator.hasNext()) {
			Entry<RepaymentHistoryHeadData, List<CalendarListDataItem>> entry = iterator
					.next();
			RepaymentHistoryHeadData key = entry.getKey();
			// 将head加入到要返回的集合里
			list.add(new RepaymentHistoryDataBean(HEAD, key));
			List<CalendarListDataItem> value = entry.getValue();
			for (int j = 0; j < value.size(); j++) {
				// 逐一将item加入到要返回的集合里
				list.add(new RepaymentHistoryDataBean(ITEM, value.get(j)));
			}
		}
		return list;
	}

}
